package www.reagnetbox.com.view;

import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.List;

import www.reagnetbox.com.R;
import www.reagnetbox.com.bean.User;
import www.reagnetbox.com.callback.SimpleCallback;
import www.reagnetbox.com.others.Const;
import www.reagnetbox.com.util.LitePalUtils;
import www.reagnetbox.com.util.ValidateUtil;
import www.reagnetbox.com.util.XPopupUtil;

public class LoginPopup extends CenterPopupView {
    Context context;
    SimpleCallback callback;

    public LoginPopup(Context context, SimpleCallback callback) {
        super(context);
        this.context = context;
        this.callback = callback;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_login;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        EditText etUsername = findViewById(R.id.et_username);
        EditText etPwd = findViewById(R.id.et_pwd);
        findViewById(R.id.tv_back).setOnClickListener(v -> dismiss());
        findViewById(R.id.tv_login).setOnClickListener(v -> {
            if (TextUtils.isEmpty(etUsername.getText())) {
                XPopupUtil.showKnowPopup(context, "用户名不能为空");
                return;
            }
            if (TextUtils.isEmpty(etUsername.getText())) {
                XPopupUtil.showKnowPopup(context, "密码不能为空");
                return;
            }
            String username = etUsername.getText().toString();
            String pwd = etPwd.getText().toString();
            if (username.equals(Const.ADMIN_USERNAME)) {
                if (pwd.equals(SPUtils.getInstance().getString(Const.ADMIN_PWD, "admin"))) {
                    loginSuccess(Const.ADMIN_USERNAME);
                } else {
                    XPopupUtil.showKnowPopup(context, "密码错误,请重试");
                }
                return;
            }
            List<User> allUsers = LitePalUtils.findAllUsers();
            if (ValidateUtil.isListValid(allUsers)) {
                for (User user : allUsers) {
                    if (user.getUsername().equals(username)) {
                        if (user.getPwd().equals(pwd)) {
                            loginSuccess(etUsername.getText().toString());
                            return;
                        } else {
                            XPopupUtil.showKnowPopup(context, "密码错误,请重试");
                            return;
                        }
                    }
                }
                XPopupUtil.showKnowPopup(context, "该用户名不存在");
            } else {
                XPopupUtil.showKnowPopup(context, "该用户名不存在");
            }
        });
    }

    private void loginSuccess(String username) {
        dismiss();
        SPUtils.getInstance().put(Const.CUR_USERNAME, username.toString());
        ToastUtils.showShort("登录成功");
        callback.fun();
    }
}
